package com.dashu.core.service;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.base.utils.CopyUtil;
import com.dashu.base.utils.UIDUtil;
import com.dashu.base.utils.VerificationUtil;
import com.dashu.core.dto.attendance.AttendanceDateDTO;
import com.dashu.core.handler.VerificationException;
import com.dashu.core.model.AttendanceDate;
import com.dashu.core.model.QAttendanceDate;
import com.dashu.core.vo.attendancek.AttendanceDateVO;
import com.querydsl.core.BooleanBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.persistence.FlushModeType;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AttendanceDateService extends DataService  {


    @Transactional(value = Transactional.TxType.REQUIRES_NEW)
    public AttendanceDateDTO add(AttendanceDateVO attendanceDateVO) {
        AttendanceDate attendanceDate = CopyUtil.copyEnty(attendanceDateVO, AttendanceDate.class);
        try {
            long userId = Long.parseLong(VerificationUtil.getUserId());
            if (attendanceDateVO.getId() == 0)
                attendanceDate.setId(UIDUtil.nextId());
            if (attendanceDateVO.getCreateUser() == 0)
                attendanceDate.setCreateUser(userId);
            if (attendanceDateVO.getCreateTime() == 0)
                attendanceDate.setCreateTime(System.currentTimeMillis());
            entityManager.persist(attendanceDate);
//            entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);


        } catch (Exception e) {
            e.printStackTrace();
            throw new VerificationException(ReplRyCode.DATA_NOT_INVALID, ReplRyCode.DATA_NOT_INVALID.getMessage());
        }
        AttendanceDateDTO attendanceDateDTO = CopyUtil.copyEnty(attendanceDate, AttendanceDateDTO.class);
        return attendanceDateDTO;

    }

    @Transactional
    public boolean deleteAttendanceData(long attendanceRuleId) {
        QAttendanceDate qAttendanceDate = QAttendanceDate.attendanceDate;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qAttendanceDate.ruleId.eq(attendanceRuleId));
        List<AttendanceDate> attendanceDates =
                queryFactory
                        .select(qAttendanceDate)
                        .from(qAttendanceDate)
                        .where(booleanBuilder.getValue())
                        .fetch();
        attendanceDates.stream().forEach(ad ->{
            entityManager.remove(ad);
//            entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);
    });

        return true;

    }

    @Transactional
    public boolean updataAttendanceData(List<AttendanceDateVO> attendanceDateVOS, long attendanceRuleId) throws Exception {
        long userId = Long.parseLong(VerificationUtil.getUserId());
        deleteAttendanceData(attendanceRuleId);
        attendanceDateVOS.stream().forEach(ad -> {
            ad.setUpdateUser(userId);
            ad.setUpdateTime(new Date().getTime());
            add(ad);
        });

        return true;

    }


    public List<AttendanceDateDTO> findAttendanceGroup(long attendanceRuleId) {
        QAttendanceDate qAttendanceDate = QAttendanceDate.attendanceDate;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qAttendanceDate.ruleId.eq(attendanceRuleId));
        List<AttendanceDate> attendanceDates =
                queryFactory
                        .select(qAttendanceDate)
                        .from(qAttendanceDate)
                        .where(booleanBuilder.getValue())
                        .fetch();
        List<AttendanceDateDTO> attendanceDateDTOS = CopyUtil.copyList(attendanceDates, AttendanceDateDTO.class);
        return attendanceDateDTOS;

    }

    public List<AttendanceDateDTO> findAttendanceTime(long timeId) {
        QAttendanceDate qAttendanceDate = QAttendanceDate.attendanceDate;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qAttendanceDate.timeId.eq(timeId));
        List<AttendanceDate> attendanceDates =
                queryFactory
                        .select(qAttendanceDate)
                        .from(qAttendanceDate)
                        .where(booleanBuilder.getValue())
                        .fetch();
        List<AttendanceDateDTO> attendanceDateDTOS = CopyUtil.copyList(attendanceDates, AttendanceDateDTO.class);
        return attendanceDateDTOS;

    }

}
